package com.jumi.microservice.controller.taobao;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelCommonException;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.dto.taobao.TaoBaoOrder;
import com.jumi.microservice.service.ITaoBaoAdminService;
import com.jumi.microservice.service.taobao.TaoBaoOrderExcelListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Dirk
 * @Date 2021-01-13 15:53
 */
@RestController
@RequestMapping("/taobao")
@Api(tags = {"淘宝"})
public class TaoBaoAdminController extends BaseController {

    @DubboReference
    private ITaoBaoAdminService taoBaoAdminService;

    @PostMapping("/order/import")
    @ApiOperation("淘宝订单导入")
    public ResponseResult<Boolean> importExcel(MultipartFile file) {
        if (file == null || file.isEmpty() || ObjectUtils.isEmpty(file.getOriginalFilename())) {
            return success(false);
        }
        String fileName = file.getOriginalFilename();
        if (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls")) {
            return success(false);
        }
        try {
            EasyExcel.read(file.getInputStream(), TaoBaoOrder.class,
                    new TaoBaoOrderExcelListener(taoBaoAdminService)).sheet().doRead();
        } catch (ExcelAnalysisException | ExcelCommonException | IOException e) {
            throw new BaseException(500, "文件异常，请检查确认");
        }
        return success(true);
    }
}
